"""
Case Type   : 资源池化系统视图
Case Name   : 配置mes相关配置在较小值，查看相关系统视图中DMSDrcContext的内存统计信息
Create At   : 2025/2
Owner       : haomeng
Description :
    1、查看mes相关配置默认值
    2、设置相关参数值为较小值
    3、主备节点分别查看系统视图pg_total_memory_detail中DMSDrcContext字段是否存在
    4、主备节点分别查看系统表gs_shared_memory_detail中DMSDrcContext字段是否存在
    5、主备节点分别查看系统表gs_thread_memory_context中DMSMemContext字段是否存在
    6、主备节点分别查看关联函数dbe_perf.track_memory_context_detail信息
    7、恢复初始环境
Expect      :
    1、查看成功
    2、设置成功
    3、无相关信息
    4、存在DMSDrcContext相关字段及信息
    5、存在DMSMemContext相关字段及信息
    6、无相关信息
    7、环境恢复成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class ShareStorageSystemView(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.user_node = [CommonSH('PrimaryDbUser'), CommonSH('Standby1DbUser')]
        self.constant = Constant()

        self.param1 = 'ss_interconnect_channel_count'
        self.param2 = 'ss_work_thread_count'
        self.param3 = 'ss_work_thread_pool_attr'
        self.param4 = 'ss_recv_msg_pool_size'

        self.sys_view1 = 'pg_total_memory_detail'
        self.sys_view2 = 'gs_total_memory_detail'
        self.sys_view3 = 'gs_shared_memory_detail'
        self.sys_view4 = 'pg_shared_memory_detail()'
        self.sys_view5 = 'gs_thread_memory_context'
        self.sys_view6 = 'gs_session_memory_detail'
        self.sys_view7 = 'gs_get_thread_memctx_detail'
        self.sys_view8 = 'dbe_perf.track_memory_context'
        self.sys_view9 = 'dbe_perf.track_memory_context_detail()'

    def test_ss_system_view(self):
        text = '-----step1:查看mes相关配置默认值  expect:查看成功-----'
        self.log.info(text)

        self.param_list = [self.param1, self.param2, self.param3, self.param4]
        value_list = ['1', '16', "''", '32MB']

        self.default_value = []
        for param in self.param_list:
            value = self.user_node[0].execut_db_sql(f'show {param}')
            tmp = value.splitlines()[-2].strip() if value.splitlines()[-2].strip() else "''"
            self.default_value.append(tmp)
        self.log.info(self.default_value)

        text = '-----step2:设置相关参数值为较小值  expect:设置成功-----'
        self.log.info(text)

        for param in self.param_list:
            set_param = self.user_node[0].execute_gsguc('set',
                                                        self.constant.GSGUC_SUCCESS_MSG,
                                                        f"{param} = {value_list[self.param_list.index(param)]}")
            self.log.info(set_param)
            self.assertTrue(set_param, '参数设置失败')

        self.log.info('---重启集群使参数生效---')
        restart = self.user_node[0].restart_db_cluster(False)
        self.log.info(restart)
        self.assertTrue(restart)

        self.log.info('---查看参数值修改是否成功---')
        for param in self.param_list:
            value = self.user_node[0].execut_db_sql(f'show {param};')
            res = value.splitlines()[-2].strip() if value.splitlines()[-2].strip() else "''"
            self.assertEqual(res, value_list[self.param_list.index(param)], '参数未修改成功')

        text = '-----step3:主备节点查看系统视图pg_total_memory_detail中DMSDrcContext字段' \
               '是否存在  expect:无相关信息-----'
        self.log.info(text)

        for node in self.user_node:
            sql1 = f"select * from {self.sys_view1} where memorytype='DMSDrcContext';"
            self.log.info(sql1)
            res1 = node.execut_db_sql(sql1)
            self.log.info(res1)
            self.assertIn('(0 rows)', res1, '存在异常信息')

        for node in self.user_node:
            sql2 = f"select * from {self.sys_view2} where memorytype='DMSDrcContext';"
            self.log.info(sql2)
            res2 = node.execut_db_sql(sql2)
            self.log.info(res2)
            self.assertIn('(0 rows)', res2, '存在异常信息')

        text = '-----step4:主备节点查看系统表gs_shared_memory_detail中DMSDrcContext字段' \
               '是否存在  expect:存在DMSDrcContext相关字段及信息-----'
        self.log.info(text)

        for node in self.user_node:
            sql3 = f"select * from {self.sys_view3} where contextname='DMSDrcContext';"
            self.log.info(sql3)
            res3 = node.execut_db_sql(sql3)
            self.log.info(res3)
            self.assertIn('DMSDrcContext', res3.splitlines()[-2], '存在异常信息')

        for node in self.user_node:
            sql4 = f"select * from {self.sys_view4} where contextname = 'DMSDrcContext';"
            self.log.info(sql4)
            res4 = node.execut_db_sql(sql4)
            self.log.info(res4)
            self.assertIn('DMSDrcContext', res4.splitlines()[-2], '存在异常信息')

        text = '-----step5:主备节点查看系统表gs_thread_memory_context中DMSMemContext字段' \
               '是否存在  expect:存在DMSMemContext相关字段及信息-----'
        self.log.info(text)

        for node in self.user_node:
            sql5 = f"select * from {self.sys_view5} where contextname = 'DMSMemContext';"
            self.log.info(sql5)
            res5 = node.execut_db_sql(sql5)
            self.log.info(res5)
            self.assertIn('DMSMemContext', res5, '存在异常信息')

            sql6 = f"select * from {self.sys_view6} where contextname = 'DMSMemContext';"
            self.log.info(sql6)
            res6 = node.execut_db_sql(sql6)
            self.log.info(res6)
            self.assertIn('DMSMemContext', res6.splitlines()[-2], '存在异常信息')

            tid = res6.splitlines()[-2].split('|')[0].split('.')[-1]
            sql7 = f"select * from {self.sys_view7}({tid}, 'DMSMemContext');"
            self.log.info(sql7)
            res7 = node.execut_db_sql(sql7)
            self.log.info(res7)
            self.assertIn('ss_dms_callback.cpp', res7, '存在异常信息')

        text = '-----step6:主备节点查看系统表dbe_perf.track_memory_context_detail中' \
               'DMSDrcContext字段是否存在  expect:无相关信息-----'
        self.log.info(text)

        for node in self.user_node:
            sql8 = f"select * from {self.sys_view8}('DMSDrcContext');"
            self.log.info(sql8)
            res8 = node.execut_db_sql(sql8)
            self.log.info(res8)
            self.assertEqual('t', res8.splitlines()[-2].strip(), '存在异常信息')

            res9 = node.execut_db_sql(f"select * from {self.sys_view9};", dbname='postgres')
            self.log.info(res9)
            self.assertIn('(0 rows)', res9, '存在异常信息')

    def tearDown(self):
        text = '-----step7:恢复初始环境  expect:恢复成功-----'
        self.log.info(text)

        for param in self.param_list:
            reset_param = self.user_node[0].execute_gsguc('set',
                                                          self.constant.GSGUC_SUCCESS_MSG,
                                                          f'{param} = {self.default_value[self.param_list.index(param)]}')
            self.log.info(reset_param)

        restart = self.user_node[0].restart_db_cluster(False)
        self.log.info(restart)

        for param in self.param_list:
            reset_value = self.user_node[0].execut_db_sql(f'show {param};')
            self.log.info(reset_value)
            res = reset_value.splitlines()[-2].strip() if reset_value.splitlines()[-2].strip() else "''"
            self.assertEqual(res, self.default_value[self.param_list.index(param)], '参数恢复失败')
        self.log.info(f'----{os.path.basename(__file__)} end----')
